package com.dobbinsoft.iotechn.data.client;

import com.dobbinsoft.iotechn.core.util.SHAUtil;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;

import java.net.URLEncoder;
import java.util.Set;
import java.util.TreeSet;
import java.util.stream.Collectors;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: rize
 * Date: 2020/4/29
 * Time: 9:59
 */
public class UninotifyAdminNotifyClient implements AdminNotifyClient {

    @Value("${com.dobbinsoft.framework.notify.uni.app-id}")
    private String appId;

    @Value("${com.dobbinsoft.framework.notify.uni.app-secret}")
    private String appSecret;

    @Value("${com.dobbinsoft.framework.notify.uni.url}")
    private String serverUrl;

    private OkHttpClient okHttpClient = new OkHttpClient();

    private static final Logger logger = LoggerFactory.getLogger(UninotifyAdminNotifyClient.class);

//    @Override
//    public void newOrder(OrderMessageEntity orderMessageEntity, String userId) {
//        try {
//            FormBody formBody = new FormBody.Builder()
//                    .add("_gp", "template")
//                    .add("_mt", "newOrderNotify")
//                    //要通知的管理员Id
//                    .add("userId", "admin")
//                    .add("orderNo", orderMessageEntity.getOrderNo())
//                    .add("actualPrice", "￥" + (orderMessageEntity.getActualPrice() / 100.0))
//                    .add("payChannel", orderMessageEntity.getPayChannel())
//                    .add("consignee", StringUtils.isEmpty(orderMessageEntity.getConsignee()) ? "" : orderMessageEntity.getConsignee())
//                    .add("phone", StringUtils.isEmpty(orderMessageEntity.getPhone()) ? "" : orderMessageEntity.getPhone())
//                    .add("address", StringUtils.isEmpty(orderMessageEntity.getAddress()) ? "" : orderMessageEntity.getAddress())
//                    .add("skuInfo", orderMessageEntity.getOrderSkuList().stream().map(item -> (item.getSpuTitle() + "-" + item.getTitle() + " * " + item.getNum())).collect(Collectors.joining("\r\n")))
//                    .add("appId", this.appId)
//                    .add("timestamp", System.currentTimeMillis() + "")
//                    .build();
//            String sign = getSign(formBody);
//            String string = okHttpClient.newCall(new Request.Builder().url(serverUrl + "?sign=" + sign).post(formBody).build()).execute().body().string();
//            logger.info(string);
//        } catch (Exception e) {
//            logger.error("[通知管理员] 异常", e);
//        }
//    }


    private String getSign(FormBody formBody) throws Exception {
        Set<String> sortSet = new TreeSet<>();
        for (int i = 0; i < formBody.size(); i++) {
            sortSet.add(formBody.value(i));
        }
        sortSet.add(this.appSecret);
        return SHAUtil.shaEncode(URLEncoder.encode(sortSet.stream().collect(Collectors.joining()), "utf-8"));
    }
}
